﻿using System;
using System.Linq;
using SilentWebsite.Model.Contracts;

namespace SilentWebsite.Model.Services
{
    public class AuthenticationServiceClient : IAuthenticationService
    {
        public event EventHandler<AuthenticationEventArgs> StatusChanged;
        protected virtual void OnStatusChanged(AuthenticationEventArgs e)
        {
            EventHandler<AuthenticationEventArgs> handler = StatusChanged;
            if (handler != null)
                handler(this, e);
        }

        public void SignIn(string username, string password)
        {
            ServicesUnitOfWork.Current.Entity.Load(ServicesUnitOfWork.Current.Entity.SignInQuery(username, password), x =>
                    {
                        var user = x.Entities.FirstOrDefault();
                        OnStatusChanged(new AuthenticationEventArgs(user, user != null));
                    }, true);
        }
        public void SignOut()
        {
            OnStatusChanged(new AuthenticationEventArgs(null, false));
        }
    }
}
